package com.chenchen.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.chenchen.subject.application.convert.SubjectAnswerDtoConverter;
import com.chenchen.subject.application.convert.SubjectInfoDtoConverter;
import com.chenchen.subject.application.dto.SubjectInfoDto;
import com.chenchen.subject.common.entity.PageResult;
import com.chenchen.subject.common.entity.Result;
import com.chenchen.subject.domain.entity.SubjectAnswerBo;
import com.chenchen.subject.domain.entity.SubjectInfoBo;
import com.chenchen.subject.domain.serivice.SubjectInfoDomainService;
import com.chenchen.subject.infra.basic.entity.SubjectInfoEs;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName SubjectController
 * description:
 * 刷题controller
 *
 * @author 海问香
 * @createTime 2024/7/12 17:22
 */
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {


    @Resource
    private RocketMQTemplate rocketMQTemplate;
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;


    /**
     * 新增题目
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDto.getSubjectName()),
                    "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDto.getCategoryIds())
                    , "分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDto.getLabelIds())
                    , "标签id不能为空");

            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDto);
            List<SubjectAnswerBo> subjectAnswerBoS =
                    SubjectAnswerDtoConverter.INSTANCE.convertListDtoToBo(subjectInfoDto.getOptionList());
            subjectInfoBo.setOptionList(subjectAnswerBoS);
            subjectInfoDomainService.add(subjectInfoBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增题目失败");
        }
    }

    /**
     * 查询题目列表
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDto>> getSubjectPage(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkNotNull(subjectInfoDto.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getLabelId(), "标签id不能为空");
            //将SubjectInfoDto转换为SubjectInfoBo
            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDto);
            //设置SubjectInfoBo的分页信息
            subjectInfoBo.setPageNo(subjectInfoDto.getPageNo());
            subjectInfoBo.setPageSize(subjectInfoDto.getPageSize());
            //调用subjectInfoDomainService的getSubjectPage方法，获取SubjectInfoBo的分页结果
            PageResult<SubjectInfoBo> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBo);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("分页查询题目失败");
        }
    }

    /**
     * 查询题目信息
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDto> querySubjectInfo(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkNotNull(subjectInfoDto.getId(), "题目id不能为空");
            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDto);
            SubjectInfoBo boResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBo);
            SubjectInfoDto dto = SubjectInfoDtoConverter.INSTANCE.convertBoToDto(boResult);
            return Result.ok(dto);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");
        }
    }

    /**
     * 全文检索
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDto.getKeyWord()), "关键词不能为空");
            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDto);
            subjectInfoBo.setPageNo(subjectInfoDto.getPageNo());
            subjectInfoBo.setPageSize(subjectInfoDto.getPageSize());
            PageResult<SubjectInfoEs> boPageResult = subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBo);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.fail("全文检索失败");
        }
    }

    /**
     * 获取题目贡献榜
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDto>> getContributeList() {
        try {
            List<SubjectInfoBo> boList = subjectInfoDomainService.getContributeList();
            List<SubjectInfoDto> dtoList = SubjectInfoDtoConverter.INSTANCE.convertBoToDtoList(boList);
            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getContributeList.error:{}", e.getMessage(), e);
            return Result.fail("获取贡献榜失败");
        }
    }

    /**
     * 测试mq发送
     * @param id
     * @return
     */
    @RequestMapping("/pushMessage")
    public Result<Boolean> get(@RequestParam("id") int id) {
        rocketMQTemplate.convertAndSend("first-topic","你好,mq" + id);
        return Result.ok(true);
    }
}